﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Linq.Expressions;

namespace FrameworkLib.Filters
{
    [Serializable]
    public class FilterBase : IFilterBase
    {
        private DateTime? m_periodFrom = null;
        private DateTime? m_periodTo = null;
        private bool m_useDates = false;

        public bool UseDates
        {
            get { return m_useDates; }
            set 
            {  
                m_useDates = value;

                //if ((m_useDates) && (m_periodFrom == null) && (m_periodTo == null))
                //{
                //    m_periodFrom = new DateTime(DateTime.Now.Year, DateTime.Now.Month, 01, 0, 0, 0);
                //    m_periodTo = new DateTime(DateTime.Now.Year, DateTime.Now.Month, DateTime.Now.Day, 23, 59, 0);
                //}
            }
        }

        public DateTime? PeriodFrom
        {
            get
            {
                if (!m_useDates) return null;

                if(m_periodFrom != null)
                  m_periodFrom = new DateTime(m_periodFrom.Value.Year, m_periodFrom.Value.Month, m_periodFrom.Value.Day, 0, 0, 0);

                return m_periodFrom;

            }
            set { m_periodFrom = value; }
        }

        public DateTime? PeriodTo
        {
            get
            {
                if (!m_useDates) return null;

                if (m_periodTo != null)
                  m_periodTo = new DateTime(m_periodTo.Value.Year, m_periodTo.Value.Month, m_periodTo.Value.Day, 23, 59, 0);
                
                return m_periodTo;
            }
            set { m_periodTo = value; }
        }

        public FilterBase()
        {

        }
    }

    [Serializable]
    public abstract class FilterBase<T> : FilterBase, IFilterBase<T>
    {
        public FilterBase()
        {
        }

        public abstract Expression<Func<T, bool>> SetUpFilter();
    }

    public static class FilterBaseExtension
    {
        public static string ToTraceString(this FilterBase filterClass)
        {
            string retValue = string.Empty;

            if (filterClass.UseDates)
            {
                retValue = string.Format("Intervallo date da {0} a {1}; ", filterClass.PeriodFrom, filterClass.PeriodTo);
            }

            System.Reflection.PropertyInfo[] pis = filterClass.GetType().GetProperties();

            System.Reflection.PropertyInfo[] almostAllProperties = pis.Where(p => p.Name != "UseDates"
                                                                             && p.Name != "PeriodFrom"
                                                                             && p.Name != "PeriodTo").ToArray();

            for (int i = 0; i < almostAllProperties.Length; i++)
            {
                if (almostAllProperties[i].GetValue(filterClass, null) != null)
                {
                    retValue += string.Format("{0}: {1}; ", almostAllProperties[i].Name, almostAllProperties[i].GetValue(filterClass, null));
                }
            }

            return retValue;
        }

        private static object DefaultValue(Type myType)
        {
            if (!myType.IsValueType)
                return null;
            else
                return Activator.CreateInstance(myType);
        }
    }
}
